package com.net.model.base;


import com.net.model.bean.BaseResponse;
import com.net.model.utils.Constant;

/**
 * Created by leo
 * on 2019/10/16.
 */
//这个用来拓展LiveData
public class Resource<T> {
    //状态  这里有多个状态 0表示加载中；1表示成功；2表示失败 ; 3加载完成
    public static final int LOADING = 0;
    public static final int SUCCESS = 1;
    public static final int FAIL = 2;
    public static final int PROGRESS = 3;//注意只有下载文件和上传图片时才会有
    public int state;

    private Integer code;//错误码
    private String message;//错误信息

    public T data;

    //这里和文件和进度有关了
    public int precent;//文件下载百分比
    public long total;//文件总大小

    public Resource(int state, T data, Integer code) {
        this.state = state;
        this.code = code;
        this.data = data;
    }

    public Resource(int state, Integer code, String message) {
        this.state = state;
        this.code = code;
        this.message = message;
    }


    public Resource(int state, int precent, long total) {
        this.state = state;
        this.precent = precent;
        this.total = total;
    }


    public static <T> Resource<T> response(BaseResponse<T> data) {
        if (data != null) {
            if ( data.getCode() == Constant.CODE_SUCCESS) {
                return new Resource<>(SUCCESS, data.getData(), data.getCode());
            }
            if (data.getCode() != Constant.CODE_SUCCESS) {
                return new Resource<>(FAIL, data.getCode(), data.getMessage());
            }
            return new Resource<>(FAIL, data.getCode(), data.getMessage());
        }
        return new Resource<>(FAIL, 0, "");
    }

    public static <T> Resource<T> response(T data) {
        if (data != null) {
            return new Resource<>(SUCCESS, data, Constant.CODE_SUCCESS);
        }
        return new Resource<>(FAIL, 0, "");
    }

    public static <T> Resource<T> responseDB(T data) {
        if (data != null) {
            return new Resource<>(SUCCESS, data, Constant.CODE_SUCCESS);
        }
        return new Resource<>(FAIL, 0, "");
    }

    /**
     * description: 开始加载数据
     *
     * @param
     * @author: zhouhequan
     * @date: 2023/3/29 11:28
     * @return: com.model.model.base.Resource<T>
     */

    public static <T> Resource<T> loading() {
        return new Resource<>(LOADING, 0, "加载开始");
    }

    /**
     * description: 数据加载失败
     *
     * @param data 失败原因
     * @author: zhouhequan
     * @date: 2023/3/29 11:29
     * @return: com.model.model.base.Resource<T>
     */

    public static <T> Resource<T> failure(BaseResponse<T> data) {
        return new Resource<>(FAIL, data.getCode(), data.getMessage());
    }

    public static <T> Resource<T> failure(Integer status, String message) {
        return new Resource<>(FAIL, status, message);
    }

    /**
     * description: 数据加载进度
     *
     * @param precent
     * @param total
     * @author: zhouhequan
     * @date: 2023/3/29 11:29
     * @return: com.model.model.base.Resource<T>
     */

    public static <T> Resource<T> progress(int precent, long total) {
        return new Resource<>(PROGRESS, precent, total);
    }

    public void handler(OnHandleCallback<T> callback) {
        handlerUnCloseDialog(callback);
        if (state != LOADING) {
            callback.onCompleted();
        }
    }


    //网络加载完成后不消失dialog;(场景：连续请求2个网络。第二个网络要等第一个网络返回的参数，才请求。第一个网络不该关闭dialog)
    public void handlerUnCloseDialog(OnHandleCallback<T> callback) {
        switch (state) {
            case LOADING:
                callback.onLoading();
                break;
            case SUCCESS:
                callback.onSuccess(data);
                break;
            case FAIL:
                callback.onFailure(code, message);
                break;
            case PROGRESS:
                callback.onProgress(precent, total);
                break;
        }
    }


    public interface OnHandleCallback<T> {
        void onLoading();

        void onSuccess(T data);

        void onFailure(Integer code, String msg);

        void onCompleted();

        void onProgress(int precent, long total);

    }


}
